/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.usc.ee.java;

/**
 * Copyright 2010 Zhou Zhao
 *
 * This file is part of FPGA compiler for EE680 USC
 *
   FPGA compiler is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FPGA compiler is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * The code in class of Netlist is used for importing netlist from txt file.
 * The code uses regular expression to import netlist, which simplifies the format
 * of each statement. Although there are some blanks between signal name and pin
 * number, code can also read in the netlist. The output of this importing process
 * is a map data structure, which has key and value pair. Each key is a signal
 * name with corresponding set of switchboxes stored in value.
 *
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.regex.MatchResult;

/**
 *
 * @author zhouzhao
 * @version 1.0
 */

public class Config{

    private Integer liCount;
    private Integer siCount;

    private Integer li_clb_num_p1;
    private Integer li_io_pin_num;

    private Integer li_t1_track_num;
    private Integer li_t2_track_num;
    private Integer li_t3_track_num;
    private Integer li_t4_track_num;

    public Config(){

    }

    public Integer getLi(){
        return this.liCount;
    }

    public Integer getSi(){
        return this.siCount;
    }

    public Integer getCLB(){
        return this.li_clb_num_p1;
    }

    public Integer getIO(){
        return this.li_io_pin_num;
    }

    public Integer getT1(){
        return this.li_t1_track_num;
    }

    public Integer getT2(){
        return this.li_t2_track_num;
    }

    public Integer getT3(){
        return this.li_t3_track_num;
    }

    public Integer getT4(){
        return this.li_t4_track_num;
    }

    public void configProcess() throws IOException{
        // TODO code application logic here

        BufferedReader inputStream = null;
        PrintWriter outputStream = null;

        try{

            inputStream = new BufferedReader(new FileReader("./FPGA_Config.txt"));
            String line;
            while((line = inputStream.readLine()) != null){
                boolean li = line.contains("LICOUNT");
                boolean si = line.contains("SICOUNT");
                boolean clb = line.contains("LI_CLB_NUM_P1");
                boolean io = line.contains("LI_IO_PIN_NUM");
                boolean t1 = line.contains("LI_T1_TRACK_NUM");
                boolean t2 = line.contains("LI_T2_TRACK_NUM");
                boolean t3 = line.contains("LI_T3_TRACK_NUM");
                boolean t4 = line.contains("LI_T4_TRACK_NUM");

                if(li){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LICOUNT\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    liCount = new Integer(result.group(1));
                    System.out.println("LICOUNT="+liCount);
                    sc.close();
                }

                if(si){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*SICOUNT\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    siCount = new Integer(result.group(1));
                    System.out.println("SICOUNT="+siCount);
                    sc.close();
                }


                if(clb){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_CLB_NUM_P1\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_clb_num_p1 = new Integer(result.group(1));
                    System.out.println("LI_CLB_NUM_P1="+li_clb_num_p1);
                    sc.close();
                }

                if(io){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_IO_PIN_NUM\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_io_pin_num = new Integer(result.group(1));
                    System.out.println("LI_IO_PIN_NUM="+li_io_pin_num);
                    sc.close();
                }

                if(t1){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_T1_TRACK_NUM\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_t1_track_num = new Integer(result.group(1));
                    System.out.println("LI_T1_TRACK_NUM="+li_t1_track_num);
                    sc.close();
                }

                if(t2){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_T2_TRACK_NUM\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_t2_track_num = new Integer(result.group(1));
                    System.out.println("LI_T2_TRACK_NUM="+li_t2_track_num);
                    sc.close();
                }

                if(t3){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_T3_TRACK_NUM\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_t3_track_num = new Integer(result.group(1));
                    System.out.println("LI_T3_TRACK_NUM="+li_t3_track_num);
                    sc.close();
                }

                if(t4){
                    Scanner sc = new Scanner(line);
                    sc.findInLine("\\s*LI_T4_TRACK_NUM\\s*=\\s*(\\d+)\\s*\\w*");
                    MatchResult result = sc.match();
                    li_t4_track_num = new Integer(result.group(1));
                    System.out.println("LI_T4_TRACK_NUM="+li_t4_track_num);
                    sc.close();
                }
            }
        }

        finally{
            if(inputStream != null){
                inputStream.close();
            }
            if(outputStream != null){
                outputStream.close();
            }
        }

    }
}
